package com.hdoit.kylin.logical.group.dao.impl;
import java.util.List;

import org.springframework.stereotype.Component;

import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.logical.superbase.model.Employee;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.message.SearchItem;
import com.tennetcn.common.utils.FinderExpressionFactory;
import com.hdoit.kylin.logical.group.dao.IGroupDao;
import com.hdoit.kylin.logical.group.model.Group;
import com.hdoit.kylin.logical.viewmodel.GroupSearch;
/**
* @author luozhicheng
* @email
* @createtime 2017年07月18日 20:43:06
* @comment
*/
@Component
public class GroupDaoImpl extends SuperDao<Group> implements IGroupDao{
	
	@Override
	public List<Group> queryList(GroupSearch search, PagerModel pagerModel){
		
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("grop.*,employee.employeeName as dutyName")
						.from(Group.class,"grop")
						.leftJoin(Employee.class,"employee")
						.on("grop.dutyId","employee.employeeId");
					
		this.append(search,finderExpression);
		if(pagerModel == null){
			return queryList(finderExpression,search);
		}
		return queryList(finderExpression,search,pagerModel);
	}

	private void append(GroupSearch search, IFinderExpression finderExpression) {
		List<SearchItem> searchItems = search.getSearchItemList();
		for(SearchItem searchItem:searchItems){
			if(searchItem.getSearchValue()!=null){
				finderExpression.andWhere("#{searchColumn}=#{searchValue}")
								.setParam("searchColum", searchItem.getColumnName())
								.setParam("searchValue", searchItem.getSearchValue());
			}
		}
		
		if(search.getDutyId() != null){
			finderExpression.andWhere("grop.dutyId = #{dutyId}")
							.setParam("dutyId", search.getDutyId());
		}
		
	}
}
